package main

import (
	"github.com/gin-gonic/gin"
	"html/template"
	"time"
)

type Student struct {
	Name string
	Age  int
}

func loopp(context *gin.Context) {
	nowtime := time.Now().Unix()
	context.HTML(200, "loop.html", gin.H{
		"now":        nowtime,
		"user":       "yun",
		"state":      "在线",
		"booksSlice": []string{"金瓶梅", "聊斋", "剪灯新话", "国色天香"},
		"stuMap": map[string]interface{}{
			"name":  "rain",
			"age":   32,
			"hobby": []string{"足球", "篮球", "双色球"},
		},
		//"stuStruct": Student{Name: "yuan", Age: 22},
		"stuStruct": []Student{{Name: "yuan", Age: 22}, {Name: "hankai", Age: 32}, {Name: "okma", Age: 52}},
	})
}
func index(context *gin.Context) {
	nowtime := time.Now().Unix()
	context.HTML(200, "index.html", gin.H{
		"now":        nowtime,
		"user":       "yun",
		"state":      "在线",
		"booksSlice": []string{"金瓶梅", "聊斋", "剪灯新话", "国色天香"},
		"stuMap": map[string]interface{}{
			"name":  "rain",
			"age":   32,
			"hobby": []string{"足球", "篮球", "双色球"},
		},
		//"stuStruct": Student{Name: "yuan", Age: 22},
		"stuStruct": []Student{{Name: "yuan", Age: 22}, {Name: "hankai", Age: 32}, {Name: "okma", Age: 52}},
	})
}
func variable(context *gin.Context) {
	nowtime := time.Now().Unix()
	context.HTML(200, "variable_assignment.html", gin.H{
		"now":        nowtime,
		"user":       "yun",
		"state":      "在线",
		"booksSlice": []string{"金瓶梅", "聊斋", "剪灯新话", "国色天香"},
		"stuMap": map[string]interface{}{
			"name":  "rain",
			"age":   30,
			"hobby": []string{"足球", "篮球", "双色球"},
		},
		//"stuStruct": Student{Name: "yuan", Age: 22},
		"stuStruct": []Student{{Name: "yuan", Age: 22}, {Name: "hankai", Age: 32}, {Name: "okma", Age: 52}},
	})
}

func main() {
	// 基于获取引擎对象，可以理解为路由对象
	r := gin.Default()
	//自定义模板函数
	r.SetFuncMap(template.FuncMap{
		"add": func(x, y int) int {
			return x + y
		},
	})
	// 加载模板文件
	r.LoadHTMLGlob("templates/*")

	r.GET("/loopp", loopp)
	r.GET("/index", index)
	r.GET("/var", variable)

	// 启动:默认本机的8080端口
	r.Run()
}
